home *** CD-ROM | disk | FTP | other *** search
/ Magnum One / Magnum One (Mid-American Digital) (Disc Manufacturing).iso / d12 / ctutor.arc / CHAP4.TXT < prev    next >
Text File  |  1988-02-28  |  32KB  |  719 lines

  1.                  Chapter 4 - Assignment & Logical compares
  2.  
  3.  
  4.                        INTEGER ASSIGNMENT STATEMENTS
  5.  
  6.              Load the file INTASIGN.C and display it for an  example 
  7.         of  assignment statements.   Three variables are defined for 
  8.         use  in the program and the rest of the program is merely  a 
  9.         series of illustrations of various assignments.   The  first 
  10.         two  lines  of  the assignment statements  assign  numerical 
  11.         values  to "a" and "b",  and the next four lines  illustrate 
  12.         the  five  basic arithmetic functions and how to  use  them.  
  13.         The  fifth is the modulo operator and gives the remainder if 
  14.         the two variables were divided.   It can only be applied  to 
  15.         "int"  or  "char"  type  variables,   and  of  course  "int" 
  16.         extensions such as "long",  "short",  etc.  Following these, 
  17.         there  are two lines illustrating how to combine some of the 
  18.         variables  in  some complex math expressions.   All  of  the 
  19.         above examples should require no comment except to say  that 
  20.         none  of  the equations are meant to be particularly  useful 
  21.         except as illustrations.
  22.  
  23.              The  next  two expressions are perfectly acceptable  as 
  24.         given,  but we will see later in this chapter that there  is 
  25.         another way to write these for more compact code.
  26.  
  27.              This leaves us with the last two lines which may appear 
  28.         to  you  as being very strange.   The C compiler  scans  the 
  29.         assignment  statement from right to left,  (which may seem a 
  30.         bit odd since we do not read that way),  resulting in a very 
  31.         useful construct,  namely the one given here.   The compiler 
  32.         finds the value 20, assigns it to "c", then continues to the 
  33.         left finding that the latest result of a calculation  should 
  34.         be  assigned to "b".   Thinking that the latest  calculation 
  35.         resulted in a 20,  it assigns it to "b" also,  and continues 
  36.         the leftward scan assigning the value 20 to "a" also.   This 
  37.         is a very useful construct when you are initializing a group 
  38.         of  variables.   The  last statement illustrates that it  is 
  39.         possible  to actually do some calculations to arrive at  the 
  40.         value which will be assigned to all three variables.
  41.  
  42.              The  program has no output so compiling  and  executing 
  43.         this  program  will be very uninteresting.  Since  you  have 
  44.         already  learned  how to display some integer results  using 
  45.         the "printf" function,  it would be to your advantage to add 
  46.         some output statements to this program to see if the various 
  47.         statements do what you think they should do.
  48.  
  49.              This would be a good time for a preliminary  definition 
  50.         of  a  rule to be followed in C.   The data definitions  are 
  51.         always given before any executable statements in any program 
  52.         block.   This is why the variables are defined first in this 
  53.         program  and in any C program.   If you try to define a  new 
  54.  
  55.  
  56.  
  57.                                   Page 18
  58.  
  59.  
  60.  
  61.  
  62.  
  63.  
  64.  
  65.  
  66.  
  67.                  Chapter 4 - Assignment & Logical compares
  68.  
  69.  
  70.         variable after executing some statements,  the compiler will 
  71.         issue an error.
  72.  
  73.                            ADDITIONAL DATA TYPES
  74.  
  75.              Loading and editing MORTYPES.C will illustrate how some 
  76.         additional  data  types can be used.   Once  again  we  have 
  77.         defined  a  few integer type variables which you  should  be 
  78.         fairly  familiar  with  by now,  but we have added  two  new 
  79.         types, the "char", and the "float". 
  80.  
  81.              The  "char"  type  of data is nearly the  same  as  the 
  82.         integer  except that it can only be assigned values  between 
  83.         zero and 255, since it is stored in only one byte of memory.  
  84.         The "char" type of data is usually used for ASCII data, more 
  85.         commonly  known  as  text.   The text you  are  reading  was 
  86.         originally written on a computer with a word processor  that 
  87.         stored the words in the computer one character per byte.  In 
  88.         contrast,  the  integer data type is stored in two bytes  of 
  89.         computer memory on most microcomputers. 
  90.  
  91.                               DATA TYPE MIXING
  92.  
  93.              It  would be profitable at this time to discuss the way 
  94.         C handles the two types "char" and "int".  Most functions in 
  95.         C  that are designed to operate with integer type  variables 
  96.         will work equally well with character type variables because 
  97.         they  are a form of an integer variable.   Those  functions, 
  98.         when called on to use a "char" type variable,  will actually 
  99.         promote  the "char" data into integer data before using  it.  
  100.         For this reason, it is possible to mix "char" and "int" type 
  101.         variables in nearly any way you desire.   The compiler  will 
  102.         not get confused,  but you might.  It is good not to rely on 
  103.         this too much, but to carefully use only the proper types of 
  104.         data where they should be used.
  105.  
  106.               The  second new data type is the "float" type of data, 
  107.         commonly  called floating point data.   This is a data  type 
  108.         which  usually  has a very large range,  a large  number  of 
  109.         significant digits, and a large number of computer words are 
  110.         required to store it.   The "float" data type has a  decimal 
  111.         point  associated  with it and,  on most computers,  has  an 
  112.         allowable range of from 10E-38 to 10E+38.  Not all compilers 
  113.         have  the  same available range,  so  check  your  reference 
  114.         manual for the limits  on your compiler.
  115.  
  116.                        HOW TO USE THE NEW DATA TYPES
  117.  
  118.              The  first three lines of the program assign values  to 
  119.         all nine of the defined variables so we can manipulate  some 
  120.         of the data between the different types.
  121.  
  122.  
  123.                                   Page 19
  124.  
  125.  
  126.  
  127.  
  128.  
  129.  
  130.  
  131.  
  132.  
  133.                  Chapter 4 - Assignment & Logical compares
  134.  
  135.  
  136.  
  137.              Since,  as  mentioned above,  a "char" data type is  in 
  138.         reality  an "integer" data type,  no special  considerations 
  139.         need be taken to promote a "char" to an "int",  and a "char" 
  140.         type data field can be assigned to an "int" variable.   When 
  141.         going the other way, there is no standard, so you may simply 
  142.         get garbage if the value of the integer variable is  outside 
  143.         the  range of the "char" type variable.   It will  translate 
  144.         correctly  if the value is within the range of zero to  255. 
  145.         In  the second line therefore,  when attempting to set x  (a 
  146.         char) to -27,  you may or may not get a well defined answer, 
  147.         it depends on your particular implementation of C.
  148.  
  149.              The   third   line   illustrates  the   simplicity   of 
  150.         translating an integer into a "float",  simply assign it the 
  151.         new  value  and the system will do  the  proper  conversion.  
  152.         When  going  the  other  way  however,  there  is  an  added 
  153.         complication.   Since  there may be a fractional part of the 
  154.         floating  point number,  the system must decide what  to  do 
  155.         with it.  By definitions , it will truncate it.
  156.  
  157.              This program produces no output, and we haven't covered 
  158.         a way to print out "char" and "float" type variables, so you 
  159.         can't  really  get  in  to this program and  play  with  the 
  160.         results, but the next program will cover this for you.
  161.  
  162.                           LOTS OF VARIABLE TYPES
  163.  
  164.              Load the file LOTTYPES.C and display it on your screen.  
  165.         This file contains every standard simple data type available 
  166.         in the programming language C.   There are other types,  but 
  167.         they are the compound types that we will cover in due time.
  168.  
  169.              Observe  the  file.   First we define a  simple  "int", 
  170.         followed  by a "long int" and a "short int".   Consult  your 
  171.         reference  manual for an exact definition of these for  your  
  172.         compiler,    because   they   are   not   consistent    from 
  173.         implementation  to implementation.   The "unsigned" is  next 
  174.         and  is  defined as the same size as the "int" but  with  no 
  175.         sign.   The "unsigned" then will cover a range of 0 to 65535 
  176.         on most microcomputers.   It should be pointed out that when 
  177.         the "long",  "short", or "unsigned" is desired, the "int" is 
  178.         optional  and  is left out by most experienced  programmers.  
  179.         We  have already covered the "char" and the  "float",  which 
  180.         leaves  only the "double".   The "double" usually  covers  a 
  181.         greater  range  than  the "float" and has  more  significant 
  182.         digits for more precise calculations.  It also requires more 
  183.         memory  to store a value than the  simple  "float".  Consult 
  184.         your  reference  manual  for the range and accuracy  of  the 
  185.         "double".
  186.  
  187.  
  188.  
  189.                                   Page 20
  190.  
  191.  
  192.  
  193.  
  194.  
  195.  
  196.  
  197.  
  198.  
  199.                  Chapter 4 - Assignment & Logical compares
  200.  
  201.  
  202.              Another  diversion  is in order at  this  point.   Most 
  203.         compilers  have no provisions for floating point  math,  but 
  204.         only  double  floating  point math.   They  will  promote  a 
  205.         "float"   to  a  "double"  before  doing  calculations   and 
  206.         therefore only one math library will be needed.   Of course, 
  207.         this  is totally transparent to you,  so you don't  need  to 
  208.         worry  about  it.   You may think that it would be  best  to 
  209.         simply define every floating point variable as double, since 
  210.         they  are promoted before use in any calculations,  but that 
  211.         may not be a good idea.  A "float" variable requires 4 bytes 
  212.         of storage and a "double" requires 8 bytes of storage, so if 
  213.         you have a large volume of floating point data to store, the 
  214.         "double"  will  obviously require much  more  memory.   Your 
  215.         compiler  may require a different number of bytes than 4  or 
  216.         8.   Consult your reference manual for the correct number of 
  217.         bytes used by your compiler.
  218.          
  219.              After  defining the data types,  a numerical  value  is 
  220.         assigned  to  each  of  the defined variables  in  order  to 
  221.         demonstrate the means of outputting each to the monitor. 
  222.  
  223.                             THE CONVERSION CHARACTERS
  224.  
  225.              Following  is a list of the conversion  characters  and 
  226.         the way they are usedin the "printf" statement.
  227.  
  228.              d    decimal notation
  229.              o    octal notation
  230.              x    hexadecimal notation
  231.              u    unsigned notation
  232.              c    character notation
  233.              s    string notation
  234.              f    floating point notation
  235.  
  236.              Each  of  these  is used following a  percent  sign  to 
  237.         indicate  the type of output conversion,  and between  those 
  238.         two characters, the following  fields may be added.
  239.  
  240.              -    left justification in its field
  241.              (n)  a number specifying minimum field width
  242.              .    to separate n from m
  243.              (m)  significant fractional digits for a float
  244.              l    to indicate a "long"
  245.  
  246.              These  are all used in the examples which are  included 
  247.         in the program presently displayed on your monitor, with the 
  248.         exception of the string notation which will be covered later 
  249.         in this tutorial.   Compile and run this program to see what 
  250.         effect the various fields have on the output.
  251.  
  252.  
  253.  
  254.  
  255.                                   Page 21
  256.  
  257.  
  258.  
  259.  
  260.  
  261.  
  262.  
  263.  
  264.  
  265.                  Chapter 4 - Assignment & Logical compares
  266.  
  267.  
  268.              You  now  have the ability to display any of  the  data 
  269.         fields  in  the  previous programs and it would be  to  your 
  270.         advantage  to go back and see if you can display any of  the 
  271.         fields anyway you desire.
  272.  
  273.                               LOGICAL COMPARES
  274.  
  275.              Load  and  view  the file  named  COMPARES.C  for  many 
  276.         examples  of compare statements in C.   We begin by defining 
  277.         and  initializing  nine variables to use  in  the  following 
  278.         compare  statements.   This initialization is new to you and 
  279.         can be used to initialize variables while they are defined.
  280.  
  281.              The  first  group of compare statements represents  the 
  282.         simplest  kinds  of compares since they simply  compare  two 
  283.         variables.    Either  variable  could  be  replaced  with  a 
  284.         constant and still be a valid compare,  but two variables is 
  285.         the general case.  The first compare checks to see if "x" is 
  286.         equal  to  "y"  and it uses the double equal  sign  for  the 
  287.         comparison.   A single equal sign could be used here but  it 
  288.         would have a different meaning as we will see shortly.   The 
  289.         second  comparison checks to see if "x" is greater than "z".  
  290.  
  291.              The   third   introduces  the   "NOT"   operator,   the 
  292.         exclamation,  which can be used to invert the result of  any 
  293.         logical  compare.   The  fourth checks for "b" less than  or 
  294.         equal to "c",  and the last checks for "r" not equal to "s".  
  295.         As  we  learned in the last chapter,  if the result  of  the 
  296.         compare  is true,  the statement following the  "if"  clause 
  297.         will  be executed and the results are given in the comments.  
  298.         Note  that  "less than" and "greater than or equal  to"  are 
  299.         also available, but are not illustrated here.
  300.  
  301.              It  would be well to mention the different format  used 
  302.         for the "if" statement in this example program.   A carriage 
  303.         return  is  not  required as a statement  separator  and  by 
  304.         putting the conditional clause on the same line as the "if", 
  305.         it adds to the readability of the overall program.
  306.  
  307.                                MORE COMPARES
  308.  
  309.              The  compares  in  the  second group  are  a  bit  more 
  310.         involved.  Starting with the first compare, we find a rather 
  311.         strange  looking set of conditions in the  parentheses.   To 
  312.         understand  this  we must understand just what a  "true"  or 
  313.         "false"  is in the C language.   A "false" is defined  as  a 
  314.         value  of zero,  and "true" is defined as a non-zero  value.  
  315.         Any  integer  or char type of variable can be used  for  the 
  316.         result of a true/false test, or the result can be an implied 
  317.         integer or char.
  318.  
  319.  
  320.  
  321.                                   Page 22
  322.  
  323.  
  324.  
  325.  
  326.  
  327.  
  328.  
  329.  
  330.  
  331.                  Chapter 4 - Assignment & Logical compares
  332.  
  333.  
  334.              Look  at  the  first  compare of the  second  group  of 
  335.         compare statements.   The expression "r != s" will  evaluate 
  336.         as  a "true" since "r" was set to 0.0 above,  so the  result 
  337.         will be a non-zero value,  probably 1.   Even though the two 
  338.         variables  that  are  compared are  "float"  variables,  the 
  339.         result  will  be of type "integer".   There is  no  explicit 
  340.         variable  to which it will be assigned so the result of  the 
  341.         compare  is  an  implied  integer.   Finally  the  resulting 
  342.         number,  1 in this case, is assigned to the integer variable 
  343.         "x".   If double equal signs were used,  the phantom  value, 
  344.         namely 1,  would be compared to the value of "x",  but since 
  345.         the  single  equal  sign  is used,  the value  1  is  simply 
  346.         assigned  to  "x",  as  though the  statement  were  not  in 
  347.         parentheses.  Finally, since the result of the assignment in 
  348.         the  parentheses  was non-zero,  the  entire  expression  is 
  349.         evaluated as "true",  and "z" is assigned the value of 1000.  
  350.         Thus  we  accomplished  two things  in  this  statement,  we 
  351.         assigned  "x" a new value,  probably 1,  and we assigned "z" 
  352.         the  value of 1000.   We covered a lot in this statement  so 
  353.         you  may wish to review it before going on.   The  important 
  354.         things  to  remember are the values that define  "true"  and 
  355.         "false", and the fact that several things can be assigned in 
  356.         a  conditional  statement.   The value assigned to  "x"  was 
  357.         probably a 1 but different compilers may assign a  different 
  358.         value as long as it is non-zero. 
  359.  
  360.              The next example should help clear up some of the above 
  361.         in your mind.  In this example, "x" is assigned the value of 
  362.         "y",  and since the result is 11, the condition is non-zero, 
  363.         which is true,   and the variable "z" is therefore  assigned 
  364.         222.
  365.  
  366.              The third example, in the second group, compares "x" to 
  367.         zero.   If  the result is true,  meaning that if "x" is  not 
  368.         zero,  then "z" is assigned the value of 333,  which it will 
  369.         be.   The  last  example in this group illustrates the  same 
  370.         concept,  since the result will be true if "x" is  non-zero.  
  371.         The compare to zero is not actually needed and the result of 
  372.         the compare is true.   The third and fourth examples of this 
  373.         group are therefore identical.
  374.  
  375.                         ADDITIONAL COMPARE CONCEPTS
  376.  
  377.              The   third  group  of  compares  will  introduce  some 
  378.         additional  concepts,  namely  the  logical  "AND"  and  the 
  379.         logical  "OR".   We  assign  the value of 77  to  the  three 
  380.         integer  variables  simply to get started  again  with  some 
  381.         defined  values.   The  first  compare of  the  third  group 
  382.         contains  the new control "&&",  which is the logical "AND".  
  383.         The  entire statement reads,  if "x" equals "y" AND  if  "x" 
  384.  
  385.  
  386.  
  387.                                   Page 23
  388.  
  389.  
  390.  
  391.  
  392.  
  393.  
  394.  
  395.  
  396.  
  397.                  Chapter 4 - Assignment & Logical compares
  398.  
  399.  
  400.         equals  77 then the result is "true".   Since this is  true, 
  401.         the variable z is set equal to 33. 
  402.  
  403.              The  next  compare  in this group introduces  the  "||" 
  404.         operator which is the "OR".   The statement reads, if "x" is 
  405.         greater  than  "y"  OR if "z" is greater than  12  then  the 
  406.         result is true.   Since "z" is greater than 12,  it  doesn't 
  407.         matter  if "x" is greater than "y" or not,  because only one 
  408.         of  the  two conditions must be true for the  result  to  be 
  409.         true.  The result is true, so therefore "z" will be assigned 
  410.         the value of 22.
  411.  
  412.                              LOGICAL EVALUATION
  413.  
  414.              When a compound expression is evaluated, the evaluation 
  415.         proceeds from left to right and as soon as the result of the 
  416.         outcome is assured,  evaluation stops.   Namely, in the case 
  417.         of  an "AND" evaluation,  when one of the terms evaluates to 
  418.         "false",  evaluation is discontinued because additional true 
  419.         terms  cannot make the result ever become  "true".   In  the 
  420.         case of an "OR" evaluation,  if any of the terms is found to 
  421.         be  "true",  evaluation stops because it will be  impossible 
  422.         for additional terms to cause the result to be "false".   In 
  423.         the case of additionally nested terms,  the above rules will 
  424.         be applied to each of the nested levels.
  425.  
  426.                           PRECEDENCE OF OPERATORS
  427.  
  428.              The  question will come up concerning the precedence of 
  429.         operators.   Which  operators are evaluated first and  which 
  430.         last?   There  are many rules about this topic,  which  your 
  431.         compiler  will define completely,  but I would suggest  that 
  432.         you don't worry about it at this point.   Instead,  use lots 
  433.         of parentheses to group variables,  constants, and operators 
  434.         in  a way meaningful to you.   Parentheses always  have  the 
  435.         highest  priority  and  will remove any  question  of  which 
  436.         operations will be done first in any particular statements.
  437.  
  438.              Going  on to the next example in group three,  we  find 
  439.         three  simple variables used in the conditional part of  the 
  440.         compare.   Since  all  three  are non-zero,  all  three  are 
  441.         "true",  and therefore the "AND" of the three variables  are 
  442.         true,  leading  to  the result being "true",  and "z"  being 
  443.         assigned  the value of 11.   Note that since the  variables, 
  444.         "r", "s", and "t" are "float" type variables, they could not 
  445.         be  used this way,  but they could each be compared to  zero 
  446.         and the same type of expression could be used. 
  447.  
  448.              Continuing on to the fourth example of the third  group 
  449.         we  find three assignment statements in the compare part  of 
  450.         the "if" statement.  If you understood the above discussion, 
  451.  
  452.  
  453.                                   Page 24
  454.  
  455.  
  456.  
  457.  
  458.  
  459.  
  460.  
  461.  
  462.  
  463.                  Chapter 4 - Assignment & Logical compares
  464.  
  465.  
  466.         you  should have no difficulty understanding that the  three 
  467.         variables are assigned their respective new values,  and the 
  468.         result  of  all three are non-zero,  leading to a  resulting 
  469.         value of "TRUE". 
  470.  
  471.                         THIS IS A TRICK, BE CAREFUL
  472.  
  473.              The last example of the third group contains a bit of a 
  474.         trick, but since we have covered it above, it is nothing new 
  475.         to you.  Notice that the first part of the compare evaluates 
  476.         to  "FALSE".   The  remaining parts of the compare  are  not 
  477.         evaluated,  because it is an "AND" and it will definitely be 
  478.         resolved as a "FALSE" because the first term is  false.   If 
  479.         the program was dependent on the value of "y" being set to 3 
  480.         in  the  next  part of the compare,  it  will  fail  because 
  481.         evaluation  will  cease following the "FALSE" found  in  the 
  482.         first  term.   Likewise,  "z" will not be set to 4,  and the 
  483.         variable "r" will not be changed. 
  484.  
  485.                           POTENTIAL PROBLEM AREAS
  486.  
  487.              The   last   group   of   compares   illustrate   three 
  488.         possibilities for getting into a bit of trouble.   All three 
  489.         have  the  common  result that "z" will not get set  to  the 
  490.         desired value,  but for different reasons.   In the case  of 
  491.         the  first  one,  the compare evaluates as "true",  but  the 
  492.         semicolon  following the second parentheses  terminates  the 
  493.         "if"  clause,  and the assignment statement involving "z" is 
  494.         always executed as the next statement.   The "if"  therefore 
  495.         has  no  effect  because of the  misplaced  semicolon.   The 
  496.         second  statement is much more straightforward  because  "x" 
  497.         will  always  be equal to itself,  therefore the  inequality 
  498.         will never be true, and the entire statement will never do a 
  499.         thing, but is wasted effort.  The last statement will always 
  500.         assign  0  to "x" and the compare will therefore  always  be 
  501.         "false",  never  executing the conditional part of the  "if" 
  502.         statement. 
  503.  
  504.              The  conditional  statement is extremely important  and 
  505.         must be thoroughly understood to write efficient C programs.  
  506.         If  any  part of this discussion is unclear  in  your  mind, 
  507.         restudy  it  until you are confident that you understand  it 
  508.         thoroughly before proceeding onward.
  509.  
  510.                            THE CRYPTIC PART OF C
  511.  
  512.              There are three constructs used in C that make no sense 
  513.         at   all  when  first  encountered  because  they  are   not 
  514.         intuitive,  but they greatly increase the efficiency of  the 
  515.         compiled  code  and  are used extensively by  experienced  C 
  516.         programmers.   You  should therefore be exposed to them  and 
  517.  
  518.  
  519.                                   Page 25
  520.  
  521.  
  522.  
  523.  
  524.  
  525.  
  526.  
  527.  
  528.  
  529.                  Chapter 4 - Assignment & Logical compares
  530.  
  531.  
  532.         learn to use them because they will appear in most,  if  not 
  533.         all,  of the programs you see in the publications.  Load and 
  534.         examine  the file named CRYPTIC.C for examples of the  three 
  535.         new constructs.
  536.  
  537.              In  this  program,   some  variables  are  defined  and 
  538.         initialized in the same statements for use below.  The first 
  539.         executable statement simply adds 1 to the value of "x",  and 
  540.         should come as no surprise to you.   The next two statements 
  541.         also  add one to the value of "x",  but it is not  intuitive 
  542.         that this is what happens.   It is simply by definition that 
  543.         this is true.  Therefore, by definition of the C language, a 
  544.         double   plus  sign  either  before  or  after  a   variable 
  545.         increments  that variable by 1.   Additionally,  if the plus 
  546.         signs are before the variable,  the variable is  incremented 
  547.         before  it  is used,  and if the plus signs  are  after  the 
  548.         variable,  the variable is used,  then incremented.   In the 
  549.         next statement, the value of "y" is assigned to the variable 
  550.         "z",  then  "y"  is incremented because the plus  signs  are 
  551.         after  the  variable  "y".   In the last  statement  of  the 
  552.         incrementing  group of example statements,  the value of "y" 
  553.         is  incremented then its value is assigned to  the  variable 
  554.         "z". 
  555.  
  556.              The  next group of statements illustrate decrementing a 
  557.         variable by one.   The definition works exactly the same way 
  558.         for decrementing as it does for incrementing.   If the minus 
  559.         signs are before the variable,  the variable is decremented, 
  560.         then  used,  and if the minus signs are after the  variable, 
  561.         the variable is used, then decremented.
  562.  
  563.                       THE CRYPTIC ARITHMETIC OPERATOR
  564.  
  565.              Another  useful but cryptic operator is the  arithmetic 
  566.         operator.   This operator is used to modify any variable  by 
  567.         some constant value.  The first statement of the "arithmetic 
  568.         operator" group of statements simply adds 12 to the value of 
  569.         the variable "a".   The second statement does the same,  but 
  570.         once again, it is not intuitive that they are the same.  Any 
  571.         of the four basic functions of arithmetic, "+", "-", "*", or 
  572.         "/",  can  be handled in this way,  by putting the  function 
  573.         desired  in  front  of the equal sign  and  eliminating  the 
  574.         second  reference to the variable name.   It should be noted 
  575.         that  the  expression on the right side  of  the  arithmetic 
  576.         operator can be any valid expression,  the examples are kept 
  577.         simple for your introduction to this new operator. 
  578.  
  579.              Just  like the incrementing and decrementing operators, 
  580.         the arithmetic operator is used extensively by experienced C 
  581.         programmers and it would pay you well to understand it.
  582.  
  583.  
  584.  
  585.                                   Page 26
  586.  
  587.  
  588.  
  589.  
  590.  
  591.  
  592.  
  593.  
  594.  
  595.                  Chapter 4 - Assignment & Logical compares
  596.  
  597.  
  598.                          THE CONDITIONAL EXPRESSION
  599.  
  600.              The  conditional expression is just as cryptic  as  the 
  601.         last  two,  but once again it can be very useful so it would 
  602.         pay you to understand it.   It consists of three expressions 
  603.         within parentheses separated by a question mark and a colon.  
  604.         The  expression prior to the question mark is  evaluated  to 
  605.         determine  if it is "true" or "false".   If it is true,  the 
  606.         expression  between  the  question mark  and  the  colon  is 
  607.         evaluated,  and if it is not true,  the expression following 
  608.         the  colon  is evaluated.   The result of the evaluation  is 
  609.         used for the assignment.   The final result is identical  to 
  610.         that  of an "if" statement with an "else" clause.   This  is 
  611.         illustrated  by  the  second example  in  this  group.   The 
  612.         conditional  expression  has  the added  advantage  of  more 
  613.         compact code that will compile to fewer machine instructions 
  614.         in the final program.
  615.  
  616.              The  final two lines of this example program are  given 
  617.         to  illustrate  a very compact way to assign the greater  of 
  618.         two variables "a" or "b" to "c", and to assign the lessor of 
  619.         the  same two variables to "c".   Notice how  efficient  the 
  620.         code is in these two examples.
  621.  
  622.                      TO BE CRYPTIC OR NOT TO BE CRYPTIC
  623.  
  624.              Several students of C have stated that they didn't like 
  625.         these  three  cryptic constructs and that they would  simply 
  626.         never  use them.   This would be fine if they never have  to 
  627.         read  anybody  else's program,  or use  any  other  programs 
  628.         within their own.  I have found many functions that I wished 
  629.         to  use within a program but needed a small modification  to 
  630.         use  it,  requiring me to understand another person's  code.  
  631.         It  would therefore be to your advantage to learn these  new 
  632.         constructs, and use them. They will be used in the remainder 
  633.         of this tutorial, so you will be constantly exposed to them.
  634.  
  635.              This has been a long chapter but it contained important 
  636.         material  to  get  you  started in using  C.   In  the  next 
  637.         chapter,  we  will go on to the building blocks  of  C,  the 
  638.         functions.  At that point, you will have enough of the basic 
  639.         materials to allow you to begin writing meaningful programs.
  640.  
  641.  
  642.         PROGRAMMING EXERCISES
  643.  
  644.         1.   Write  a program that will count from 1 to 12 and print 
  645.              the count, and its square, for each count.
  646.                   1    1
  647.                   2    4
  648.                   3    9   etc.
  649.  
  650.  
  651.                                   Page 27
  652.  
  653.  
  654.  
  655.  
  656.  
  657.  
  658.  
  659.  
  660.  
  661.                  Chapter 4 - Assignment & Logical compares
  662.  
  663.  
  664.  
  665.         2.   Write a program that counts from 1 to 12 and prints the 
  666.              count  and  its  inversion  to  5  decimal  places  for 
  667.              each count. This will require a floating point number.
  668.                   1    1.00000
  669.                   2     .50000
  670.                   3     .33333
  671.                   4     .25000
  672.                   etc.
  673.  
  674.         3.   Write a program that will count from 1 to 100 and print 
  675.              only those values between 32 and 39, one to a line.
  676.           
  677.  
  678.  
  679.  
  680.  
  681.  
  682.  
  683.  
  684.  
  685.  
  686.  
  687.  
  688.  
  689.  
  690.  
  691.  
  692.  
  693.  
  694.  
  695.  
  696.  
  697.  
  698.  
  699.  
  700.  
  701.  
  702.  
  703.  
  704.  
  705.  
  706.  
  707.  
  708.  
  709.  
  710.  
  711.  
  712.  
  713.  
  714.  
  715.  
  716.  
  717.                                   Page 28
  718.  
  719.